package org.jeecg.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.purchase.Vo.PurchaseSchemeVo;
import org.jeecg.modules.purchase.entity.PurchasePlan;
import org.jeecg.modules.purchase.mapper.PurchasePlanMapper;
import org.jeecg.modules.purchase.service.IPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class PurchasePlanServiceImpl extends ServiceImpl<PurchasePlanMapper, PurchasePlan> implements IPurchasePlanService {

    @Autowired
    private PurchasePlanMapper purchasePlanMapper;
//    @Autowired
//    private ProcurementNoticeUtil procurementNoticeUtil;

    @Override
    @Transactional
    public boolean savePurchasePlan(PurchasePlan purchasePlan) {

        purchasePlan.setCreateTime(new Date());
        return this.save(purchasePlan);
    }

    @Override
    @Transactional
    public boolean updatePurchasePlan(PurchasePlan purchasePlan) {
        purchasePlan.setUpdateTime(new Date());
        return this.updateById(purchasePlan);
    }

//    修改审核状态
    @Transactional
    public boolean auditPurchasePlan(String id, String auditStatus, String auditOpinion, String auditor) {
        // 从数据库中根据ID获取对应的purchasePlan对象
        PurchasePlan purchasePlan = this.getById(id);
        if (purchasePlan == null) {
            log.error("失败，采购方案ID不存在: {}", id);
            return false;
        }

        purchasePlan.setAuditStatus(auditStatus);
        purchasePlan.setAuditOpinion(auditOpinion);
        purchasePlan.setAuditor(auditor);
        purchasePlan.setAuditTime(new Date());

        return this.updateById(purchasePlan);
    }

    @Override
    public IPage<PurchasePlan> queryPurchasePlanPage(Page<PurchasePlan> page, PurchasePlan purchasePlan) {
        QueryWrapper<PurchasePlan> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(purchasePlan.getProjectName())) {
            queryWrapper.like("project_name", purchasePlan.getProjectName());
        }
        if (StringUtils.isNotBlank(purchasePlan.getProjectNumber())) {
            queryWrapper.like("project_number", purchasePlan.getProjectNumber());
        }
        if (StringUtils.isNotBlank(purchasePlan.getCategory())) {
            queryWrapper.eq("category", purchasePlan.getCategory());
        }
        if (StringUtils.isNotBlank(purchasePlan.getAuditStatus())) {
            queryWrapper.eq("audit_status", purchasePlan.getAuditStatus());
        }
        if (StringUtils.isNotBlank(purchasePlan.getCreateBy())) {
            queryWrapper.eq("create_by", purchasePlan.getCreateBy());
        }

        queryWrapper.orderByDesc("create_time");
        return this.page(page, queryWrapper);
    }

//    @Override
//    public boolean publishPurchasePlan(String id, String auditStatus, String auditOpinion, String auditor) {
//        PurchasePlan purchasePlan = this.getById(id);
//        if (purchasePlan == null) {
//            log.error("失败，采购方案ID不存在: {}", id);
//            return false;
//        }
//
//        purchasePlan.setAuditStatus("0");
////        purchasePlan.setAuditOpinion(auditOpinion);
////        purchasePlan.setAuditor(auditor);
//        purchasePlan.setAuditTime(new Date());
//        //TODO 如果是打回要发信息通知
//        return this.updateById(purchasePlan);
//    }

    @Override
    public PurchasePlan getApprovedPlanById(String id) {
        return purchasePlanMapper.getApprovedPlanById(id);
    }

    @Override//查看项目编号是否存在
    public boolean checkProjectNumberExists(String projectNumber) {

        return purchasePlanMapper.checkProjectNumberExists(projectNumber) > 0;
    }

    @Override
    public boolean auditpurchaseScheme(String id, String Status, String auditOpinion, String username) {
        // 从数据库中根据ID获取对应的purchaseScheme对象
        PurchasePlan purchasePlan = this.getById(id);
        if (purchasePlan == null) {
            log.error("审核失败，采购编辑ID不存在: {}", id);
            return false;
        }

        // 更新审核状态
        purchasePlan.setAuditStatus(Status);
        purchasePlan.setUpdateBy(username);
        purchasePlan.setUpdateTime(new Date());
        //TODO 如果是打回要发信息通知

        return this.updateById(purchasePlan);
    }


    @Transactional
    public boolean addPurchaseScheme(PurchaseSchemeVo purchaseSchemeVo, String username) {
        // 校验采购计划ID是否为空
        if (purchaseSchemeVo.getId() == null || purchaseSchemeVo.getId().trim().isEmpty()) {
            log.error("采购计划ID不能为空");
            return false;
        }

        // 验证采购计划是否存在且已审批
        PurchasePlan purchasePlan = this.getApprovedPlanById(purchaseSchemeVo.getId());
        if (purchasePlan == null) {
            log.error("未找到已审批的采购计划: {}", purchaseSchemeVo.getId());
            return false;
        }

        // 设置方案类型
        purchasePlan.setSchemeType(purchaseSchemeVo.getSchemeType());

        // 设置时间字段
        purchasePlan.setAnnouncementEndTime(purchaseSchemeVo.getAnnouncementEndTime());
        purchasePlan.setRegistrationEndTime(purchaseSchemeVo.getRegistrationEndTime());
        purchasePlan.setBiddingEndTime(purchaseSchemeVo.getBiddingEndTime());
        purchasePlan.setOpeningTime(purchaseSchemeVo.getOpeningTime());
        purchasePlan.setEvaluationTime(purchaseSchemeVo.getEvaluationTime());
        purchasePlan.setStatus("2");
        purchasePlan.setWinningTime(purchaseSchemeVo.getWinningTime());
        purchasePlan.setFlowTime(purchaseSchemeVo.getFlowTime());
        //设置节点公告
        purchasePlan.setExplanation(purchaseSchemeVo.getExplanation());
        purchasePlan.setAllowPricesBelowCost(purchaseSchemeVo.getAllowPricesBelowCost());
        purchasePlan.setAnnouncementExplanation(purchaseSchemeVo.getAnnouncementExplanation());
        purchasePlan.setReportExplanation(purchaseSchemeVo.getReportExplanation());
        purchasePlan.setBidExplanation(purchaseSchemeVo.getBidExplanation());
        purchasePlan.setOpenbidExplanation(purchaseSchemeVo.getOpenbidExplanation());
        purchasePlan.setEvaluationbidExplanation(purchaseSchemeVo.getEvaluationbidExplanation());
        // 设置更新人信息
        purchasePlan.setUpdateBy(username);
        purchasePlan.setUpdateTime(new Date());

        return this.updateById(purchasePlan);
    }

    @Override
    public boolean isExistPurchasePlan(String purchasePlanId) {
        Integer count = purchasePlanMapper.CountById(purchasePlanId);
        if (count == 1) return true;
        else return false;
    }
}
